package com.JFapp.service.impl;

import com.JFapp.constants.SystemConstants;
import com.JFapp.domain.dto.TagDto;
import com.JFapp.domain.dto.TagListDto;
import com.JFapp.domain.entity.ResponseResult;
import com.JFapp.domain.entity.Tag;
import com.JFapp.domain.vo.PageVo;
import com.JFapp.domain.vo.TagVo;
import com.JFapp.eumns.AppHttpCodeEnum;
import com.JFapp.mapper.TagMapper;
import com.JFapp.service.TagService;
import com.JFapp.utils.BeanCopyUtils;
import com.JFapp.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * (Tag)表服务实现类
 *
 * @author makejava
 * @since 2024-03-13 23:18:38
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {
    @Autowired
    private TagService tagService;
    @Override
    public ResponseResult selectTagList(Integer pageNum, Integer pageSize,TagListDto tagListDto) {
   /*     if (!StringUtils.hasText(tagListDto.getName())&&!StringUtils.hasText(tagListDto.getRemark())){
            return ResponseResult.errorResult(AppHttpCodeEnum.TAG_SELECT_ERROR);
        }*/
        //查询标签
        LambdaQueryWrapper<Tag> wrapper=new LambdaQueryWrapper<>();
        wrapper.select(Tag::getId,Tag::getName,Tag::getRemark);
        wrapper.eq(StringUtils.hasText(tagListDto.getName()),Tag::getName,tagListDto.getName());
        wrapper.eq(StringUtils.hasText(tagListDto.getRemark()),Tag::getRemark,tagListDto.getRemark());
        wrapper.in(Tag::getDelFlag, SystemConstants.STATUS_NORMAL);
        //分页
        Page<Tag> page=new Page<>(pageNum,pageSize);
        page(page,wrapper);
        List<Tag> tags=page.getRecords();
        PageVo pageVo=new PageVo(tags,page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addTag( TagListDto tagListDto) {
        //判断输入名称非空
        if(!StringUtils.hasText(tagListDto.getName())){
            return ResponseResult.errorResult(AppHttpCodeEnum.TAG_NAME_ERROR);
        }
        //获取用户id
        Long userId = SecurityUtils.getUserId();
        //插入新标签
        Tag tag=new Tag();
        tag.setName(tagListDto.getName());
        tag.setRemark(tagListDto.getRemark());
        tagService.save(tag);
        return ResponseResult.okResult();
    }

    @Override
    public void setTagToArticle(Long id, List<Long> tags) {
        baseMapper.setTagToArticle(id,tags);
    }

    @Override
    public ResponseResult selectALLTagList() {
        LambdaQueryWrapper<Tag> queryWrapper= new LambdaQueryWrapper<>();
        LambdaQueryWrapper<Tag> wrapper = queryWrapper.select().eq(Tag::getDelFlag, SystemConstants.NOT_DELETE);
        List<Tag> tagList = list(wrapper);
        List<TagVo> tagVoList = BeanCopyUtils.copyBeanList(tagList, TagVo.class);
        return ResponseResult.okResult(tagVoList);
    }

    @Override
    public ResponseResult updateTag(TagDto tag) {
        Tag tag1 = BeanCopyUtils.copyBean(tag, Tag.class);
        LambdaUpdateWrapper<Tag> updateWrapper=new LambdaUpdateWrapper<>();
        LambdaUpdateWrapper<Tag> wrapper = updateWrapper.eq(Tag::getId, tag1.getId());
        update(tag1,wrapper);
        return ResponseResult.okResult();
    }

}
